package top.ywlog.permission.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.ywlog.permission.common.request.DeptParam;
import top.ywlog.permission.common.response.Result;
import top.ywlog.permission.service.SysDeptService;
import top.ywlog.permission.service.SysTreeService;

import javax.annotation.Resource;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * @author: Durian
 * @date: 2020/10/18 21:05
 * @description: 部门
 */
@Slf4j
@Validated
@Controller
@RequestMapping("/sys/dept")
public class SysDeptController
{
    @Resource
    private SysDeptService sysDeptService;
    @Resource
    private SysTreeService sysTreeService;

    @PostMapping("/save")
    @ResponseBody
    public Result saveDept(@RequestBody @Validated DeptParam param)
    {
        sysDeptService.save(param);
        return Result.success();
    }

    @GetMapping("/tree")
    @ResponseBody
    public Result getTree()
    {
        return Result.success(sysTreeService.deptTree());
    }

    @PutMapping("/update")
    @ResponseBody
    public Result updateDept(@RequestBody @Validated DeptParam deptParam)
    {
        sysDeptService.update(deptParam);
        return Result.success();
    }

    @DeleteMapping("/delete/{deptId}")
    @ResponseBody
    public Result deleteDept(@NotNull(message = "参数错误")
                             @Min(value = 0, message = "参数错误")
                             @PathVariable("deptId") Integer deptId)
    {
        sysDeptService.delete(deptId);
        return Result.success();
    }

}
